﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace syc.ZhiJian
{
    public partial class 磁粉探伤废品判定管理 : System.Web.UI.Page
    {
        protected string Notice = "";
        string sql =
@"select t2.ID,t3.产品类别ID,
case when t3.产品类别ID=1
then '油管接箍(J55)'
when t3.产品类别ID=2
then '套管接箍(J55)'
when t3.产品类别ID=3
then '油管接箍(N80Q)'
when t3.产品类别ID=4
then '套管接箍(N80Q)'
end as 品名
,isnull(sum(t1.支数),0) as 废品支数,'磁粉探伤废' as 原因,t2.名称 as 供货单位,t1.炉号,t3.日期
,t1.是否判定,t1.判定日期,t3.ID as 进度ID
 from 接箍质检数据 t1
left outer join 生产进度3 t3 on t3.ID=t1.进度ID
left outer join 原料供货单位 t2 on t1.供货=t2.ID where t1.车丝机=-3

";
        string sql1 =
@"select t2.ID,t3.产品类别ID,
case when t3.产品类别ID=1
then '油管接箍(J55)'
when t3.产品类别ID=2
then '套管接箍(J55)'
when t3.产品类别ID=3
then '油管接箍(N80Q)'
when t3.产品类别ID=4
then '套管接箍(N80Q)'
end as 品名
,isnull(sum(t1.支数),0) as 废品支数,'车丝废' as 原因,t2.名称 as 供货单位,t1.炉号,t3.日期
,t1.是否判定,t1.判定日期,t3.ID as 进度ID
 from 接箍质检数据 t1
left outer join 生产进度3 t3 on t3.ID=t1.进度ID
left outer join 原料供货单位 t2 on t1.供货=t2.ID where t1.车丝机>0

";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.GongHuo.DataBind();
                this.Button_Query_Click(null, null);
            }
            else
            {
                this.SqlDataSource1.SelectCommand = this.SqlText.Text;
            }
        }

        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            this.GridView1.SelectedIndex = -1;          

            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }

        }
        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);
        }
        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            }

        }
        protected void PageSizeOption_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.GridView1.PageSize = int.Parse(this.PageSizeOption.Text);
        }
        protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {
            Label_Total.Text = "共查询到 " + e.AffectedRows + " 项记录";
        }
        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
                     

        }
        private string GetState()
        {
            string state = "";

          
            if (this.GongHuo.Text != "0")
            {
                state += " and t2.ID=" + this.GongHuo.Text;
            }
            if (this.LeiBie.Text != "0")
            {
                state += " and t3.产品类别ID=" + this.LeiBie.Text;
            }
         
            DateTime d;
            if (DateTime.TryParse(this.Date1.Text, out d))
            {
                state += " and t3.日期>='" + d.ToShortDateString() + "'";
            }
            if (DateTime.TryParse(this.Date2.Text, out d))
            {
                state += " and t3.日期<='" + d.ToShortDateString() + "'";
            }
            
            //if (state.StartsWith(" and"))
            //{
            //    state = state.Substring(4);
            //}
           
            return state;
        }
        protected void Button_Query_Click(object sender, EventArgs e)
        {           
            string state = this.GetState();
            if (this.LeiXing.Text == "1")
            {
                this.SqlText.Text = sql + state + " group by t3.日期,t3.ID,t2.ID,t3.产品类别ID,t2.名称,t1.炉号,t1.是否判定,t1.判定日期 order by t3.日期 desc";

            }
            if (this.LeiXing.Text == "2")
            {
                this.SqlText.Text = sql1 + state + " group by t3.日期,t3.ID,t2.ID,t3.产品类别ID,t2.名称,t1.炉号,t1.是否判定,t1.判定日期 order by t3.日期 desc";

            }
            this.SqlDataSource1.SelectCommand = "";
            this.SqlDataSource1.SelectCommand = this.SqlText.Text;
        }

        protected void Button_Reset_Click(object sender, EventArgs e)
        {
            this.GongHuo.Text = "0";

            this.Date1.Text = "";
            this.Date2.Text = "";

            this.LeiBie.Text = "0";
        }
        protected int Check(string check, string ID)
        {

            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            Conn.Open();
            SqlCommand Cmd = new SqlCommand();
            Cmd.Connection = Conn;
            try
            {
                string op = (string)Session["OperatorName"];
                if (op == null)
                {
                    op = "";
                }
                Cmd.CommandText = "废品接箍判定";
                Cmd.CommandType = CommandType.StoredProcedure;
                Cmd.Parameters.Add(new SqlParameter("@是否判定", SqlDbType.Bit, 20, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, check));
                Cmd.Parameters.Add(new SqlParameter("@废品原因", SqlDbType.Int, 10, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, int.Parse(this.LeiXing.Text)));
                Cmd.Parameters.Add(new SqlParameter("@进度ID", SqlDbType.Int, 10, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, int.Parse(ID)));
                
                Cmd.Parameters.Add(new SqlParameter("@返回值", SqlDbType.Int, 4, ParameterDirection.Output, true, 0, 0, "", DataRowVersion.Proposed, null));
                Cmd.ExecuteNonQuery();
            }
            catch (Exception err)
            {
                this.Message.Text = err.Message;
            }
            Cmd.Dispose();
            Conn.Close();
            Conn.Dispose();
            return (int)Cmd.Parameters["@返回值"].Value;
        }
        //合格判定
        protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            var ch = "";
            if (((CheckBox)sender).Checked)
            {
                ch = "true";
            }
            else
            {
                ch = "false";
            }
            var id = ((CheckBox)sender).ToolTip;
            var check = Check(ch, id);
            if (check == 1)
            {
                this.Message.Text = "·判定成功！";
            }
            else
            {
                this.Message.Text = "·判定失败！";
            }
            this.GridView1.SelectedIndex = -1;
            GridView1.DataBind();
        }
    }
}
